package com.chain.qs.modules.biz.web;

import com.chain.qs.common.config.Global;
import com.chain.qs.common.utils.StringUtils;
import com.chain.qs.common.web.BaseController;
import com.chain.qs.modules.biz.entity.EquipmentGroup;
import com.chain.qs.modules.biz.service.EquipmentGroupService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备组Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/equgroup")
public class EquipmentGroupController extends BaseController {

    @Resource
    private EquipmentGroupService equipmentGroupService;

    @ModelAttribute
    public EquipmentGroup get(@RequestParam(required=false) String id) {
        if (StringUtils.isNotBlank(id)){
            return equipmentGroupService.get(id);
        }else{
            return new EquipmentGroup();
        }
    }

    @RequestMapping(value = {"list",""})
    public String list(EquipmentGroup group, Model model) {
        model.addAttribute("list", equipmentGroupService.findList(group));
        return "modules/biz/equGroupList";
    }

    @RequestMapping(value="form")
    public String from(){
        return "modules/biz/equGroupForm";
    }

    @ResponseBody
    @RequestMapping(value = "save")//@Valid
    public String save(EquipmentGroup group){
        equipmentGroupService.save(group);
        return "success";
    }

    @RequestMapping(value = "delete")
    public String delete(EquipmentGroup group, RedirectAttributes redirectAttributes) {
        equipmentGroupService.delete(group);
        addMessage(redirectAttributes, "删除设备组成功");
        return "redirect:" + adminPath + "/biz/equgroup";
    }


    @RequestMapping(value = "findListByType")
    public String findListByType(HttpServletResponse response, int type) {
        List<EquipmentGroup> egList = equipmentGroupService.findListByType(type);
        return renderString(response,egList);
    }
}
